package com.example.redis.config;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: guorf
 * @time: 2021/12/3 10:46
 */
@Data
@Component //注册bean到容器中
public class RedisProperty {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port:6379}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database-list:}")
    private List<Integer> databaseList;

    @Value("${spring.redis.database:}")
    private Integer database;

    @Value("${spring.redis.ttl:7D}")
    private Duration ttl;

    // 链接超时
    @Value("${spring.redis.timeout:5000}")
    private long timeout;

    @Value("${spring.redis.lettuce.shutdown-timeout:1000}")
    private long shutDownTimeout;

    @Value("${spring.redis.lettuce.pool.max-idle:10}")
    private int maxIdle;

    @Value("${spring.redis.lettuce.pool.min-idle:5}")
    private int minIdle;

    @Value("${spring.redis.lettuce.pool.max-active:100}")
    private int maxActive;

    @Value("${spring.redis.lettuce.pool.max-wait:3000}")
    private long maxWait;

    @PostConstruct
    public void init(){
        if(CollUtil.isNotEmpty(databaseList)){
            if(database == null){
                database = databaseList.get(0);
            }
        }else if(database != null) {
            List<Integer> list = new ArrayList<Integer>();
            list.add(this.getDatabase());
            this.setDatabaseList(list);
        }else{
            throw new NullPointerException("database and databaseList cannot be empty at the same time!");
        }
    }

}
